﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccessLayer;
using System.Net.Mail;
using System.Net;

namespace BusinessLogicLayer
{
    public class ApproveRequisitionController
    {
        ApproveRequisitionDAL approveDAL = new ApproveRequisitionDAL();
        EmailNotificationControl emailContoller = new EmailNotificationControl();

        public int getRoleID(String userName)
        {
            int r = approveDAL.getRoleInfoByUserName(userName);
            return r;
        }

        //this is for getting all the employee list of pending requisition
        public List<Employee> getPendingEmpList(String userName)
        {
            return approveDAL.getPendingEmpListForDept(userName);
        }

        public List<Employee> getEmpList(String userName)
        {
            return approveDAL.getEmpListByUserName(userName);
        }

        public String getEmpNameByEmpID(int empID)
        {
            return approveDAL.getEmpNameByID(empID);
        }

        public String getEmpName(String userName)
        {
            return approveDAL.getEmpNameByUserName(userName);
        }

        public List<Requisition> getRequisition(String userName)
        {
            return approveDAL.getRequisitionByUserName(userName);
        }

        public List<object> getReqDet(int reqID)
        {
            return approveDAL.getReqDetailOneByReqID(reqID);
        }

        public List<object> getAllPendingRequisition(String userName)
        {
            return approveDAL.getAllPendingRequisitionByDept(userName);
        }

        public List<object> getPendingReq(int empId)
        {
            return approveDAL.getPendingReqByUserName(empId);
        }

        //for saving to deptrequisition and deptrequisitionDetail
        public void saveToDeptReq(String deptID, int reqID, List<string> rdoStatus, List<string> reasons)
        {
            int deptReqID = approveDAL.checkDeptID(deptID);

            if (deptReqID == 0)
            {
                deptReqID = approveDAL.saveToDeptReq(deptID);
            }

            List<RequisitionDetailOne> reqDetOne = approveDAL.getRealReqDetailOneByReqID(reqID);
            List<DeptRequisitionDetail> deptReqDetOne = approveDAL.getDeptReqDetList(deptReqID);

            for (int i = 0; i < reqDetOne.Count; i++)
            {
                if (rdoStatus[i] == "Reject")
                {
                    int reqDetID = reqDetOne[i].ReqDetailOneID;

                    approveDAL.updateReqDetOneStatus(reqDetID, "Reject", reasons[i].ToString());
                    reqDetOne.Remove(reqDetOne[i]);
                    rdoStatus.Remove(rdoStatus[i]);
                    reasons.Remove(reasons[i]);

                }
                else
                {
                    for (int j = 0; j < deptReqDetOne.Count; j++)
                    {
                        if (reqDetOne[i].ItemCode == deptReqDetOne[j].ItemCode)
                        {
                            int reqDetID = reqDetOne[i].ReqDetailOneID;

                            //update the qty in deptREquisitionDetail if the item is already in there
                            approveDAL.updateDeptReqDet(reqDetOne[i].ItemCode, Convert.ToInt32(reqDetOne[i].RequestQty), deptReqID);
                            approveDAL.updateReqDetOneStatus(reqDetID, "Approved", null);
                            reqDetOne.Remove(reqDetOne[i]);
                            break;
                        }
                    }
                }
            }

            //save to deptRequisitionDetail if the item is new
            for (int i = 0; i < reqDetOne.Count; i++)
            {
                int reqDetID = reqDetOne[i].ReqDetailOneID;
                approveDAL.saveDeptReqDet(reqDetOne[i].ItemCode, Convert.ToInt32(reqDetOne[i].RequestQty), deptReqID);
                approveDAL.updateReqDetOneStatus(reqDetID, "Approved", null);

            }
            approveDAL.updateRequistionStatus(deptID, reqID);
        }

        public String getDeptID(int empId)
        {
            return approveDAL.getDeptIDByEmpID(empId);
        }

        public void SendNotificationToEmployee(int empID, string deptID)
        {

            String headEmail = approveDAL.getHeadEmail(deptID);
            Employee emp = approveDAL.getEmpInfoByEmpID(empID);
            String empEmail = emp.Email;
            String Subject = "Your Requisition has been processed! ";
            String Body = "Dear " + emp.EmployeeName + ",<br />This is to inform you that you requisition " +
                    "has been processed. You may login to the Stationery Store Inventory Management Applicaiton to view the status of individual items." +
                    "<br />Thank you!<br />";

            emailContoller.sendNotification(empEmail, headEmail, Body, Subject);

        }

    }
}
